from django.shortcuts import *

from webs.models import User, Stu


def Login(request):
    if request.method =="GET":
        return render(request,'login.html')
    username = request.POST.get('username')
    password = request.POST.get('password')
    userinfo = User.objects.filter(username=username,password=password).first()
    if userinfo:
        request.session['username'] = userinfo.username
        request.session.set_expiry(0)
        return redirect(reverse('index'))
    return render(request,'login.html',{'msg':'帐号密码错误'})
def index(request):
    if request.method == "GET":
        stu = Stu.objects.all()
        return render(request, 'index.html',{'data':stu})


def addstu(request):
    if request.method == "GET":
        if request.session.get('username'):
            return render(request, 'add.html')
        return redirect(reverse('login'))
    name = request.POST.get('name')
    sex = request.POST.get('sex')
    xueyaun = request.POST.get('xueyaun')
    clas = request.POST.get('clas')
    zhiwu = request.POST.get('zhiwu')
    b_time = request.POST.get('b_time')
    tpl = request.POST.get('tpl')
    Stu.objects.create(name=name,sex=sex,xueyaun=xueyaun,clas=clas,zhiwu=zhiwu,b_time=b_time,tpl=tpl)
    return redirect(reverse('index'))
def updat(request,id):
    if request.method == "GET":
        if request.session.get('username'):
            stu = Stu.objects.all()
            return render(request, 'updat.html',{'stu':stu})
        return redirect(reverse('login'))
    name = request.POST.get('name')
    sex = request.POST.get('sex')
    xueyaun = request.POST.get('xueyaun')
    clas = request.POST.get('clas')
    zhiwu = request.POST.get('zhiwu')
    b_time = request.POST.get('b_time')
    tpl = request.POST.get('tpl')
    Stu.objects.create(name=name,sex=sex,xueyaun=xueyaun,clas=clas,zhiwu=zhiwu,b_time=b_time,tpl=tpl)
    return redirect(reverse('index'))
def delete(request,id):
    pass
